#import <Foundation/NSArray.h>

#import "Button.h"

@implementation Button

- (id)initWithFrame: (CGRect)frame keyCode: (NSArray *)codes
{
	self = [super initWithFrame: frame];
	
	keyCodes = codes;
	[keyCodes retain];
	
	return self;
}

- (void)dealloc
{
	[keyCodes release];
	[super dealloc];
}

- (void)setKeyDelegate: (id)delegate
{
	keyDelegate = delegate;
}

- (id)keyDelegate
{
	return keyDelegate;
}

- (void)mouseDown: (CGEventRef)event
{
	[super mouseDown: event];
	
	for (int keys = 0; keys != [keyCodes count]; ++keys)
	{
		NSArray *sets = [keyCodes objectAtIndex: keys];
		for (int set = 0; set != [sets count]; ++set)
		{
			unsigned char keyCode = [[sets objectAtIndex: set] unsignedCharValue];
			[keyDelegate keyPressed: keyCode];
		}
		
/*
		for (int set = 0; set != [sets count]; ++set)
		{
			unsigned char keyCode = [[sets objectAtIndex: set] unsignedCharValue];
			[keyDelegate keyReleased: keyCode];
		}
*/
	}
}

- (void)mouseUp: (CGEventRef)event
{
	[super mouseUp: event];
	
	for (int keys = 0; keys != [keyCodes count]; ++keys)
	{
		NSArray *sets = [keyCodes objectAtIndex: keys];
/*
		for (int set = 0; set != [sets count]; ++set)
		{
			unsigned char keyCode = [[sets objectAtIndex: set] unsignedCharValue];
			[keyDelegate keyPressed: keyCode];
		}
*/
		
		for (int set = 0; set != [sets count]; ++set)
		{
			unsigned char keyCode = [[sets objectAtIndex: set] unsignedCharValue];
			[keyDelegate keyReleased: keyCode];
		}
	}
}

@end
